package test1;

import static org.lwjgl.opengl.GL11.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.lwjgl.input.Mouse;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.*;
import org.lwjgl.*;

public class InputDemo
{
	/*********************************** BEGIN CONSTANTS *******************************************/
	public static final int WIDTH = 800;
	public static final int HEIGHT = 600;
	public static final int ZNEAR = 1;

	/*********************************** END CONSTANTS *******************************************/

	List<Box> shapes = new ArrayList<Box>(16);
	private boolean somethingIsSelected = false;
	private volatile boolean randomColorCooldown = false;

	public InputDemo()
	{
		try
		{
			Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
			Display.setTitle("Hello, LWJGL!");
			Display.create();
		} catch (LWJGLException e)
		{
			e.printStackTrace();
		}

		shapes.add(new Box(15, 15));
		shapes.add(new Box(100, 150));

		// Initialize OpenGL
		glMatrixMode(GL_PROJECTION);
		glLoadIdentity();
		glOrtho(0, WIDTH, HEIGHT, 0, ZNEAR, -1);
		glMatrixMode(GL_MODELVIEW);

		while (!Display.isCloseRequested())
		{
			glClear(GL_COLOR_BUFFER_BIT);

			while (Keyboard.next())
			{
				if (Keyboard.getEventKey() == Keyboard.KEY_C && Keyboard.getEventKeyState())
				{
					shapes.add(new Box(15, 15));
				}
			}

			if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE))
			{
				Display.destroy();
				System.exit(0);
			}

			for (Box box : shapes)
			{
				if (Mouse.isButtonDown(2) && box.inBounds(Mouse.getX(), GetMouseY()) && !somethingIsSelected)
				{
					if (!randomColorCooldown)
					{
						box.randomizeColors();
						randomColorCooldown = true;
						new Thread(new Runnable()
						{
							public void run()
							{
								try
								{									
									Thread.sleep(200);

								} catch (InterruptedException e)
								{
									e.printStackTrace();
								} finally
								{
									randomColorCooldown = false;
								}
							}
						}).run();
					}
				}

				if (Mouse.isButtonDown(0) && box.inBounds(Mouse.getX(), GetMouseY()) && !somethingIsSelected)
				{
					somethingIsSelected = true;
					box.selected = true;
					System.out.println("You clicked me");
				}
				if (Mouse.isButtonDown(1))
				{
					box.selected = false;
					somethingIsSelected = false;
				}

				if (box.selected)
				{
					box.update(Mouse.getDX(), GetMouseDY());
				}
				box.draw();
			}

			int M_Y = GetMouseDY();

			Display.update();
			Display.sync(60);
		}
		Display.destroy();
	}

	private static class Box
	{
		public boolean selected = false;
		public int x, y;
		private float colorRed, colorBlue, colorGreen;

		Box(int x, int y)
		{
			this.x = x;
			this.y = y;

			Random randomGenerator = new Random();
			colorRed = randomGenerator.nextFloat();
			colorBlue = randomGenerator.nextFloat();
			colorGreen = randomGenerator.nextFloat();
		}

		void randomizeColors()
		{
			Random randomGenerator = new Random();
			colorRed = randomGenerator.nextFloat();
			colorBlue = randomGenerator.nextFloat();
			colorGreen = randomGenerator.nextFloat();
		}

		void update(int dx, int dy)
		{
			x += dx;
			y += dy;
		}

		boolean inBounds(int mousex, int mousey)
		{
			if (mousex > x && mousex < x + 50 && mousey > y && mousey < y + 50)
				return true;
			else
				return false;
		}

		void draw()
		{
			glColor3f(colorRed, colorGreen, colorBlue);
			glBegin(GL_QUADS);
			glVertex2f(x, y);
			glVertex2f(x + 50, y);
			glVertex2f(x + 50, y + 50);
			glVertex2f(x, y + 50);
			glEnd();
		}
	}

	public static void main(String[] args)
	{
		new InputDemo();

	}

	private int GetMouseDY()
	{
		return -Mouse.getDY();
	}

	private int GetMouseY()
	{
		return HEIGHT - Mouse.getY() - 1;
	}

}
